package com.childmiles.gwt.client.ui.login;

import com.childmiles.gwt.client.Presenter;
import com.childmiles.gwt.client.domain.AwardWinner;
import com.childmiles.gwt.client.event.ShowAwardsEvent;
import com.childmiles.gwt.client.service.AwardWinnerServiceAsync;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyDownHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class LoginPresenter implements Presenter {

    private final AwardWinnerServiceAsync awardWinnerService;
    private final HandlerManager eventBus;
    private final Display display;
    
    public interface Display {
        Widget asWidget();
        HasClickHandlers getOkButton();
        HasText getAwardWinnerNameTextBox();
        HasKeyDownHandlers getAwardWinnerNameTextBoxForKeyDownHandler();
      }

    public LoginPresenter(AwardWinnerServiceAsync awardWinnerService, HandlerManager eventBus, Display display) {
        this.awardWinnerService = awardWinnerService;
        // TODO Auto-generated constructor stub
        this.eventBus = eventBus;
        this.display = display;
        bind();
    }
    
    public void bind() {
        display.getOkButton().addClickHandler(new ClickHandler() {   
          public void onClick(ClickEvent event) {
                  login(display.getAwardWinnerNameTextBox().getText());
              }
          });

        display.getAwardWinnerNameTextBoxForKeyDownHandler().addKeyDownHandler(
                new KeyDownHandler() {
                    public void onKeyDown(KeyDownEvent event) {
                        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                            login(display.getAwardWinnerNameTextBox().getText());
                        }
                    }
        });

    }

    /**
     * Find award winner and set it as currentAwardWinner
     * @param awardWinnerName
     */
    private void login(final String awardWinnerName) {
        if (awardWinnerName.isEmpty()) {
            Window.alert("Child's name required");
        }
        awardWinnerService.find(awardWinnerName,new AsyncCallback<AwardWinner>() {
            public void onFailure(Throwable caught) {
                throw new RuntimeException(caught);
            }
            public void onSuccess(AwardWinner result) {
                AwardWinner awardWinner;
                if (result == null) { //not found
                    awardWinner = new AwardWinner(awardWinnerName);
                }
                else {
                    awardWinner = result;
                }
                eventBus.fireEvent(new ShowAwardsEvent(awardWinner));
            }
        });
    }

    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }
    
    

}
